classdef GaltonEBStrategyMV < TradingStrategy
    %This is an class for the Naive Markowitz trading strategies
    properties
        name =  {'Galton EB'};
        galtonCorrection;        
    end
    
    methods
        function obj = strategyCalculate(obj, t, r_past, r_plus1, r_curr )
            
            post_window = obj.galtonCorrection.post_window;
            galton_coeffs = obj.galtonCorrection.galton_coeffs;
            
            basisMeans=mean(r_past)';
            cov_H=cov(r_past);
            corr_H=corr(r_past);
            var_H=diag(cov_H);
            
            [corr_shrink, corr_intercept] = obj.getGaltonConstrained(nanmean(galton_coeffs(1:t-post_window,2,2,1)), nanmean(galton_coeffs(1:t-post_window,1,2,1)) );
            
            corr_Gal=corr_shrink*corr_H + corr_intercept;
            corr_Gal(eye(size(corr_Gal))==1)=1;
            
            [var_shrink, var_intercept] =  obj.getGaltonConstrained(nanmean(galton_coeffs(1:t-post_window,2,3,1)),nanmean(galton_coeffs(1:t-post_window,1,3,1)) );
            
            var_Gal=var_H*var_shrink+var_intercept;
            var_Gal=max(var_Gal,0);
            
            cov_Gal=diag(var_Gal)^.5*corr_Gal*diag(var_Gal)^0.5;% the corrected covariance matrix
            
            k=min(eig(cov_Gal));
            
            if k<=0
                cov_Gal = cov_Gal + eye(size(cov_Gal))*(-2*k);
            end
            
            [mean_shrink, mean_intercept] =  obj.getGaltonConstrained(nanmean(galton_coeffs(1:t-post_window,2,4,1)), nanmean(nanmean(galton_coeffs(1:t-post_window,1,4,1))));
            
            miu_MRC=basisMeans*mean_shrink+mean_intercept;
            
            weightsMV = obj.setMVStrategyParams(cov_Gal,miu_MRC);
            obj = obj.executeCommonElements(t,weightsMV,cov_Gal,miu_MRC, r_plus1,r_curr);
            
        end
        
        function [cons_shrink, cons_intercept] = getGaltonConstrained(obj,shrink,intercept)
            
            if shrink <= 0
                cons_shrink = 0;
                cons_intercept = intercept;
            else
                if shrink >= 1
                    cons_shrink = 1;
                    cons_intercept = intercept;                
                else
                    cons_shrink = shrink;
                    cons_intercept = intercept;
               end
            end
        end
    end
end

